/***
 * 学生
 * ***/
var pageNow=1
SPA_RESOLVE_INIT = function(transition) {
    var html = '<div class="commonRight-top-select mb-10 f-14 mt-15">\n'+
        '添加方式 : '+
        '<select onchange="inputTypeChanges(this)">'+
        '<option value="0">手动添加</option>'+
        '<option value="1">文件添加</option>'+
        '</select>\n'+
        '<div id="addInputs" class="thefile" style="display: inline">'+
        '<input type="number" id="input-adduser" class="rad-3 ml-10 f-14">' +
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10 " onclick="addstudent()">确认添加</a>'+
        '</div>' +
        '</div>'+
        '<table class="Table_common newsListTab mt-20">\n'+
        '<thead>\n'+
        '<tr>\n'+
        // '<td>id</td>\n'+
        '<td>学号</td>\n'+
        '<td>密码</td>\n'+
        // '<td>姓名</td>\n'+
        '<td>电话</td>\n'+
        '<td>邮箱</td>\n'+
        // '<td>密码</td>\n'+
        '<td>操作</td>\n'+
        '</tr>\n'+
        '</thead>\n'+
        '<tbody id="newsListTabs">\n'+
        '</tbody>\n'+
        '</table>\n'+
        '<div class="paging-area-box mt-10 magin-auto">\n'+
        '<div class="paging-area float-left" id="pagination"></div>\n'+
        '<span class="pageNum float-right f-14">共<em>0</em>条数据</span>\n'+
        '</div>';
    html += '<style>\n'+
        '\n'+
        '.newsListTab tr td:nth-child(2),\n'+
        '.newsListTab tr td:nth-child(4){\n'+
        'border-left:1px solid #333;\n'+
        'border-right:1px solid #333;\n'+
        '}\n'+
        '\n'+
        '.commonRight-top-select{\n'+
        'margin-left:2%;\n'+
        '}\n'+
        '.commonRight-top-select select{\n'+
        'border-radius: 3px;\n'+
        'border:1px solid #999;\n'+
        'width:125px;\n'+
        'height:30px;\n'+
        '}\n'+
        '.commonRight-top-select a.ui-btn{\n'+
        'padding:3px 8px;\n'+
        'border:1px solid #666;\n'+
        '}\n'+
        '\n'+
        '</style>';
    getHref_path();
    $(".menu ul").hide();
    $(".menu ul").eq(5).show();
    $("#ui-right-content").html(html)
    $("#addUser").hide();
    studentListFun(pageNow)
}
function studentListFun(pageNo){
    var id = window.document.getElementById("newsListTabs");
    $.ajax({
        url: get_studentLlist_interface+'?currentPage='+pageNo,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            currentPage:pageNo
        },
        success: function(data) {
            if(data.code==200){
                inner_studentList_Tbody(data,id, studentListFun);
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}
function inner_studentList_Tbody(data,id, func){
    var page = data.data.num;
    var currentPage = data.data.currentPage;
    var tml = "";
    $(".pageNum em").html(page);
    if(page>0){
        setPagination(page,currentPage, func);
        pageNow=currentPage
        var lists = data.data.list;
        $('#pagination').show();
        for (x in lists) {
            if(lists[x]["email"]==null)lists[x]["email"]='空'
            if(lists[x]["telephone"]==null)lists[x]["telephone"]='空'
            tml += "<tr>"+
                // '<td>'+x+'</td>'+
                '<td>'+lists[x]["username"]+'</td>'+
                "<td>"+lists[x]["nickname"]+"</td>"+
                "<td>"+lists[x]["telephone"]+"</td>"+
                "<td>"+lists[x]["email"]+"</td>"+
                '<td><a href="javascript:;" class="c-009 f-14" onclick="resetUser(this)" data-username="'+lists[x]["username"]+'">重置密码</a> / '+
                '<a href="javascript:;" class="c-009 f-14" onclick="deleteUser(this)" data-username="'+lists[x]["username"]+'">删除</a></td>'+
                "</tr>" ;
        }
    }else{
        $('#pagination').hide();
    }
    id.innerHTML = tml;
}

/**编辑**/
function resetUser(obj){
    var username = $(obj).attr("data-username");
    $.ajax({
        url: reset_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            resetUsername:username,
        },
        success: function(data) {
            if(data.code==200){
                $.message("重置成功");
                studentListFun(pageNow)
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}
/**删除***/
function deleteUser(obj){
    var username = $(obj).attr("data-username");
    $.ajax({
        url: delete_user_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            username:username,
        },
        success: function(data) {
            if(data.code==200){
                $.message("删除成功");
                // $(obj).closest("tr").remove();
                studentListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

// 显示添加用户的版面
function showaddUser(that) {
    $("#addUser").toggle();
    $("#input-adduser").val("");
    if($(that).html()=="添加") {
        that.innerHTML = "取消";
    }
    else {
        that.innerHTML = "添加";
    }
}

//添加用户
function addstudent() {
    var addUsername = $("#input-adduser").val();
    $.ajax({
        url: register_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            username:addUsername,
            password:addUsername.toString(),
            checkPassword:addUsername.toString()
        },
        success: function(data) {
            if(data.code==200){
                $.message("添加成功")
                studentListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function addstudentFiles() {
    var formdata=new FormData();
    formdata.append("file",$("#fileInput")[0].files[0]);
    console.log($("#fileInput")[0].files[0])
    $.ajax({
        url: register_student_files_interface,
        type:'post',
        dataType:'json',
        cache: false,
        contentType: false,
        processData: false,
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:formdata,
        success: function(data) {
            if(data.code==200){
                $.message("添加成功")
                studentListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function inputTypeChanges(obj) {
    var html_manual='<input type="number" id="input-adduser" class="rad-3 ml-10 f-14"/>' +
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10 " onclick="addstudent()">确认添加</a>';
    var html_files='<input id="filename" type="text" class="rad-3 ml-10 f-14 outline-none c-666 filename" readonly/>' +
        '<div class="upload-box lite-upload-box relative bg-169 text-center c-fff f-14 rad-3 ml-10">' +
        '选择文件<input id="fileInput" type="file" name="files" onchange="fileinput(this)" class="absolute pointer fileinputs"/>' +
        '</div>' +
        '<a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10" onclick="addstudentFiles()">上传</a>' +
        '<span class="c-999 ml-10">文件格式 xlsx, csv, xls</span>';
    var way=$(obj).val();
    if(way=='0'){
        $("#addInputs").html(html_manual)
    }
    else {
        $("#addInputs").html(html_files)
    }
}